<template>
  <div class="app-container">
    <el-container>
      <el-aside width="300px" style="background-color: white;padding: 2px;margin-bottom: 0;">
        <!--组织结构数据-->
        <project-tree @selectProject="getCurrentProject" />
      </el-aside>
      <!--楼栋数据-->
      <el-main style="padding: 0 0 0 20px;">
        <el-form
          ref="queryForm"
          :model="queryParams1"
          :inline="true"
          label-width="68px"
        >
          <el-form-item
            size="mini"
            label=""
            prop="userName"
          >
            <el-input
              v-model="queryParams1.fnameL2"
              placeholder="请输入楼栋名称"
              clearable
              prefix-icon="el-icon-search"
              style="width: 240px"
              @input="handleQuery"
            />
          </el-form-item>
          <el-form-item size="mini">
            <el-button
              type="primary"
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
            >新增
            </el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>

        <el-table
          ref="processTableRef"
          v-loading="loading"
          highlight-current-row
          size="small"
          :data="processList"
          height="calc(100vh - 150px)"
        >
          <el-table-column
            label="楼栋编码"
            align="center"
            min-width="70"
          >
            <template slot-scope="scope">
              <el-popover
                placement="top-start"
                trigger="click"
              >
                <div class="check-edit-content">{{ scope.row.fnumber }}</div>
                <div slot="reference" class="ellipsis">{{ scope.row.fnumber }}</div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column
            label="楼栋名称"
            align="center"
            min-width="100"
          >
            <template slot-scope="scope">
              <el-popover
                placement="top-start"
                trigger="click"
              >
                <div class="check-edit-content">{{ scope.row.fnameL2 }}</div>
                <div slot="reference" class="ellipsis">{{ scope.row.fnameL2 }}</div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column label="创建人" align="center" min-width="60" prop="creatorName" />
          <el-table-column
            label="创建时间"
            align="center"
            min-width="90"
          >
            <template slot-scope="scope">
              <el-popover
                placement="top-start"
                trigger="click"
              >
                <div class="check-edit-content">{{ scope.row.fcreatetime }}</div>
                <div slot="reference" class="ellipsis">{{ scope.row.fcreatetime }}</div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="cfroompapernumber" min-width="90">
            <template slot="header">
              <el-popover
                placement="top-start"
                width="150"
                trigger="click"
                content="大产权证号\主件号"
              >
                <div slot="reference" class="ellipsis">大产权证号\主件号</div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="cfpaperbizdate" min-width="90">
            <template slot="header">
              <el-popover
                placement="top-start"
                width="150"
                trigger="click"
                content="大产权证办理时间"
              >
                <div slot="reference" class="ellipsis">大产权证办理时间</div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="cfpresalebizdate" min-width="90">
            <template slot="header">
              <el-popover
                placement="top-start"
                width="150"
                trigger="click"
                content="预售证办理时间"
              >
                <div slot="reference" class="ellipsis">预售证办理时间</div>
              </el-popover>
            </template>
          </el-table-column>
          <el-table-column align="center" min-width="60" label="产品类型">
            <template slot-scope="scope">
              <span>{{ scope.row.fproducttypeid | productType }}</span>
            </template>
          </el-table-column>
          <el-table-column
            label="操作"
            align="center"
            width="250"
            class-name="small-padding fixed-width"
          >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-edit"
                @click="handleUpdate(scope.row)"
              >修改
              </el-button>
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="handleDelete(scope.row)"
              >删除
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-main>
    </el-container>

    <!-- 修改参数配置对话框 -->
    <el-dialog
      :title="title"
      :close-on-click-modal="false"
      :visible.sync="open"
      width="900px"
      append-to-body
      @close="reset"
    >
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        size="mini"
        label-width="120px"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item
              label="楼栋编码"
              prop="fnumber"
            >
              <el-input
                v-model="form.fnumber"
                placeholder="请输入"
                class="form-input"
              />
            </el-form-item>
            <el-form-item
              label="产品类型"
              prop="fproducttypeid"
            >
              <el-select
                v-model="form.fproducttypeid"
                filterable
                placeholder="请选择"
                class="form-input"
              >
                <el-option
                  v-for="type in initData.productTypeEnum"
                  :key="type.fid"
                  :label="type.fnameL2"
                  :value="type.fid"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="建筑结构" prop="fbuildingstructureId">
              <el-select
                v-model="form.fbuildingstructureId"
                placeholder="请选择"
                class="form-input"
              >
                <el-option
                  v-for="type in initData.buildingStructureEnum"
                  :key="type.fid"
                  :label="type.fnameL2"
                  :value="type.fid"
                />
              </el-select>
            </el-form-item>
            <el-form-item label="开工日期" prop="fstartdate">
              <el-date-picker
                v-model="form.fstartdate"
                value-format="yyyy-MM-dd"
                type="date"
                class="form-input"
                placeholder="选择日期"
              />
            </el-form-item>
            <el-form-item label="计划封顶日期" prop="cfplancapsdate">
              <el-date-picker
                v-model="form.cfplancapsdate"
                value-format="yyyy-MM-dd"
                type="date"
                class="form-input"
                placeholder="选择日期"
              />
            </el-form-item>
            <el-form-item label="计划竣工日期" prop="cfplancompletedate">
              <el-date-picker
                v-model="form.cfplancompletedate"
                value-format="yyyy-MM-dd"
                type="date"
                class="form-input"
                placeholder="选择日期"
              />
            </el-form-item>
            <el-form-item
              label="占地面积"
              prop="fbuildingterraarea"
            >
              <el-input
                v-model="form.fbuildingterraarea"
                placeholder="请输入"
                class="form-input"
              >
                <template slot="append">㎡</template>
              </el-input>
            </el-form-item>
            <el-form-item
              label="楼高"
              prop="fbuildingheight"
            >
              <el-input
                v-model="form.fbuildingheight"
                placeholder="请输入"
                class="form-input"
              >
                <template slot="append">m</template>
              </el-input>
            </el-form-item>
            <el-form-item
              label="地上楼层数"
              prop="ffloorcount"
            >
              <el-input-number v-model="form.ffloorcount" class="form-input" controls-position="right" :min="1" />
            </el-form-item>
            <el-form-item
              label="大产权证号\主件号"
              prop="cfroompapernumber"
            >
              <el-input
                v-model="form.cfroompapernumber"
                placeholder="请输入"
                class="form-input"
              />
            </el-form-item>
            <el-form-item
              label="使用率"
              prop="fuserate"
            >
              <el-input
                v-model="form.fuserate"
                placeholder="请输入"
                class="form-input"
              >
                <template slot="append">%</template>
              </el-input>
            </el-form-item>
            <el-form-item prop="fisgetcertificated">
              <el-checkbox v-model="form.fisgetcertificated" :true-label="1" :false-label="0">是否已取得销售许可证</el-checkbox>
            </el-form-item>
            <el-form-item label="" prop="cfissale">
              <el-checkbox v-model="form.cfissale" :true-label="1" :false-label="0">是否可售</el-checkbox>
            </el-form-item>
            <el-form-item label="" prop="cfisparkinglot">
              <el-checkbox v-model="form.cfisparkinglot" :true-label="1" :false-label="0">是否车位</el-checkbox>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item
              label="楼栋名称"
              prop="fnameL2"
            >
              <el-input
                v-model="form.fnameL2"
                placeholder="请输入"
                class="form-input"
              />
            </el-form-item>
            <el-form-item>
              <div style="height: 28px;" />
            </el-form-item>
            <el-form-item label="建筑性质" prop="fbuildingpropertyid">
              <el-select
                v-model="form.fbuildingpropertyid"
                placeholder="请选择"
                class="form-input"
                filterable
              >
                <el-option
                  v-for="type in initData.buildingPeropertyEnum"
                  :key="type.fid"
                  :label="type.fnameL2"
                  :value="type.fid"
                />
              </el-select>
            </el-form-item>
            <el-form-item>
              <div style="height: 28px;" />
            </el-form-item>
            <el-form-item label="封顶日期" prop="cfcapsdate">
              <el-date-picker
                v-model="form.cfcapsdate"
                value-format="yyyy-MM-dd"
                type="date"
                class="form-input"
                placeholder="选择日期"
              />
            </el-form-item>
            <el-form-item label="竣工日期" prop="fcompletedate">
              <el-date-picker
                v-model="form.fcompletedate"
                value-format="yyyy-MM-dd"
                type="date"
                class="form-input"
                placeholder="选择日期"
              />
            </el-form-item>
            <el-form-item
              label="建筑面积"
              prop="fbuildingarea"
            >
              <el-input
                v-model="form.fbuildingarea"
                placeholder="请输入"
                class="form-input"
              >
                <template slot="append">㎡</template>
              </el-input>
            </el-form-item>
            <el-form-item
              label="层高"
              prop="ffloorheight"
            >
              <el-input
                v-model="form.ffloorheight"
                placeholder="请输入"
                class="form-input"
              >
                <template slot="append">m</template>
              </el-input>
            </el-form-item>
            <el-form-item
              label="地下楼层数"
              prop="fsubfloorcount"
            >
              <el-input-number v-model="form.fsubfloorcount" class="form-input" controls-position="right" :max="0" />
            </el-form-item>
            <el-form-item label="大产权证办理时间" prop="cfpaperbizdate">
              <el-date-picker
                v-model="form.cfpaperbizdate"
                value-format="yyyy-MM-dd"
                type="date"
                class="form-input"
                placeholder="选择日期"
              />
            </el-form-item>
            <el-form-item label="工程达售时间" prop="cfpresalereadydate">
              <el-date-picker
                v-model="form.cfpresalereadydate"
                value-format="yyyy-MM-dd"
                type="date"
                class="form-input"
                placeholder="选择日期"
              />
            </el-form-item>
            <el-form-item
              label="达售楼层"
              prop="salefloor"
            >
              <el-input-number v-model="form.salefloor" class="form-input" controls-position="right" />
            </el-form-item>
            <el-form-item label="预售证办理时间" prop="cfpresalebizdate">
              <el-date-picker
                v-model="form.cfpresalebizdate"
                value-format="yyyy-MM-dd"
                type="date"
                class="form-input"
                placeholder="选择日期"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div
        slot="footer"
        class="dialog-footer"
      >
        <el-button
          type="primary"
          @click="submitForm"
        >确 定
        </el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  initBuildingManage,
  listBuildingManage,
  delBuildingManage,
  addBuildingManage,
  updateBuildingManage
} from '@/api/baseManage/buildingManage'
import ProjectTree from './components/ProjectTree'
const form = {
  fnumber: '',
  fproducttypeid: '',
  fbuildingstructureId: '',
  fstartdate: '',
  fbuildingterraarea: '',
  fbuildingheight: '',
  ffloorcount: '',
  cfroompapernumber: '',
  fuserate: '',
  fisgetcertificated: 0,
  fnameL2: '',
  fbuildingpropertyid: '',
  fcompletedate: '',
  fbuildingarea: '',
  ffloorheight: '',
  fsubfloorcount: '',
  cfpaperbizdate: '',
  cfpresalebizdate: '',
  cfcapsdate: '',
  cfplancapsdate: '',
  cfplancompletedate: '',
  cfpresalereadydate: '',
  cfissale: 0,
  cfisparkinglot: 0,
  salefloor: 0,
  publicareatype: null
}
let productTypeEnum = []
export default {
  name: 'BuildingManage',
  components: {
    ProjectTree
  },
  filters: {
    productType(val) {
      for (let i = 0; i < productTypeEnum.length; i++) {
        if (val === productTypeEnum[i].fid) {
          return productTypeEnum[i].fnameL2
        }
      }
      return val
    }
  },
  data() {
    return {
      // 遮罩层
      loading: false,
      // 楼栋表格数据
      processList: [],
      originalList: [],
      // 弹出层标题
      title: '',
      // 部门树选项
      // 是否显示弹出层
      open: false,
      // 表单参数
      form: { ...form },
      queryParams1: {
        fnameL2: ''
      },
      // 查询参数
      queryParams: {
        cfcurprojectid: null
      },
      // 表单校验
      rules: {
        fnameL2: [
          { required: true, message: '楼栋名称不能为空', trigger: 'blur' }
        ],
        // fproducttypeid: [
        //   { required: true, message: '请选择产品类型', trigger: 'change' }
        // ],
        ffloorcount: [
          { required: true, message: '地上楼层不能为空', trigger: 'blur' }
        ],
        fsubfloorcount: [
          { required: true, message: '地下楼层不能为空', trigger: 'blur' }
        ]
      },
      specialTypes: [],
      checkTypeList: [],
      currentProject: {},
      roles: [],
      users: [],
      initData: {
        buildingPeropertyEnum: [],
        buildingStructureEnum: [],
        productTypeEnum: []
      }
    }
  },
  created() {
    // 测试使用
    // this.queryParams.cfcurprojectid = '14BhQdVOSwyOLB45K3wGb/nl6Ss='
    // this.currentProject.id = '14BhQdVOSwyOLB45K3wGb/nl6Ss='
    // this.getList()
    this.init()
  },
  methods: {
    // 页面初始化
    init() {
      initBuildingManage(this.queryParams).then(
        (response) => {
          this.initData = response.data
          productTypeEnum = [...response.data.productTypeEnum]
        }
      ).catch(() => {
        this.loading = false
      })
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams1.fnameL2 = ''
      this.getList()
    },
    getCurrentProject(data) {
      console.log(data)
      if (data.type === 'project') {
        this.queryParams.cfcurprojectid = data.id
        this.currentProject = data
        this.getList()
      }
    },
    /** 查询楼栋列表 */
    getList() {
      this.loading = true
      var params = JSON.parse(JSON.stringify(this.queryParams))
      params['typeIds'] = '0'
      listBuildingManage(params).then(
        (response) => {
          this.processList = response.data
          this.originalList = [...response.data]
          this.loading = false
          // this.handleQuery()
        }
      ).catch(() => {
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
    },
    // 表单重置
    reset() {
      this.$refs['form'].resetFields()
      this.form = { ...form }
    },

    handleQuery() {
      this.processList = this.originalList.filter(item => item.fnameL2 && item.fnameL2.indexOf(this.queryParams1.fnameL2) !== -1)
    },
    /** 新增按钮操作 */
    handleAdd() {
      if (!this.currentProject.id) {
        this.$message.warning('请选择项目')
        return
      }
      this.form.cfcurprojectid = this.currentProject.id
      this.open = true
      this.title = '新增'
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.open = true
      this.title = '修改'
      this.form = { ...row }
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          if (this.form.fid) {
            this.form.publicareatype = 0
            updateBuildingManage(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess('修改成功')
                this.open = false
                this.getList()
              }
            })
          } else {
            addBuildingManage(this.form).then((response) => {
              if (response.code === 200) {
                this.msgSuccess('新增成功')
                this.open = false
                this.getList()
              }
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$confirm(
        `是否要删除楼栋：${row.fnameL2} 的数据项?`,
        '警告',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      )
        .then(function() {
          return delBuildingManage(row.fid)
        })
        .then(() => {
          this.getList()
          this.msgSuccess('删除成功')
        })
        .catch(function() {
        })
    }
  }
}
</script>
<style type="scss" scoped>
  .aside-scrollbar {
    height: calc(100vh - 150px);
  }
  .ellipsis {
    width: 100%;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    cursor: pointer;
  }
  .check-content {
    max-width: 50vw;
    font-size: 12px;
    color: #606266;
  }
  .check-type {
    font-size: 16px;
    color: #606266;
    padding: 10px;
    border-bottom: 1px solid #dcdfe6;
  }
  .check-edit-content {
    max-width: 600px;
    font-size: 12px;
    color: #606266;
  }
  .form-input {
    width: 260px;
  }
</style>
